import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { userStorer } from "@/stores/user_store";
import NProgress from "nprogress";
import { Message } from "@arco-design/web-vue";

const routes: Array<RouteRecordRaw> = [
  //   children: [
  //     {
  //       path: "employee",
  //       name: "PersonEmployee",
  //       component: () => import("@/views/person/employee/index.vue"),
  //     }
  //   ]
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/web",
    name: "Web",
    component: () => import("@/views/web/index.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/admin",
    name: "Admin",
    meta: {
      title: "首页",
    },
    component: () => import("@/views/admin/index.vue"),
    children: [
      {
        path: "home",
        name: "Home",
        meta: {
          title: "首页",
          role: [1, 2],
        },
        component: () => import("@/views/admin/home/index.vue"),
      },
      {
        path: "map",
        name: "Map",
        meta: {
          title: "地图",
          role: [1, 2],
        },
        children: [
          {
            path: "gaode",
            name: "Gaode",
            meta: {
              title: "高德地图",
            },
            component: () => import("@/views/admin/map/gaode/index.vue"),
          },
          {
            path: "baidu",
            name: "Baidu",
            meta: {
              title: "百度地图",
            },
            component: () => import("@/views/admin/map/baidu/index.vue"),
          },
          {
            path: "tenxun",
            name: "Tenxun",
            meta: {
              title: "腾讯地图",
            },
            component: () => import("@/views/admin/map/tenxun/index.vue"),
          },
        ],
      },
      {
        path: "system",
        name: "System",
        meta: {
          title: "系统",
          role: [1],
        },
        children: [
          {
            path: "user",
            name: "SystemUser",
            meta: {
              title: "用户管理",
            },
            component: () => import("@/views/admin/system/user/index.vue"),
          },
          {
            path: "role",
            name: "SystemRole",
            meta: {
              title: "角色管理",
            },
            component: () => import("@/views/admin/system/role/index.vue"),
          },
        ],
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 路由加载前
// router.beforeEach((to, from, next) => {
//   const token = LocalCache.get("token");
//   if (to.path === "/login" && !token) {
//     next();
//   } else {
//     if (!token) {
//       LocalCache.clear();
//       next(`/login`);
//     } else if (token && to.path === "/login") {
//       next("/index");
//     } else {
//       next();
//     }
//   }
// });

router.beforeEach((to, from, next) => {
  if (to.meta.role) {
    const userStore = userStorer();
    if (!userStore.isLogin) {
      Message.error("请先登录");
      router.push({ name: "Login", query: { redirect: to.path } });
      return;
    }

    if (!to.meta?.role.includes(userStore.userInfo.role)) {
      Message.error("没有权限");
      router.push(from.path);
      return;
    }
  }

  NProgress.configure({ showSpinner: false });
  NProgress.start();
  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
